﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Data;
using System.Security;
using System.Globalization;
using System.Security.Permissions;

namespace Light.Utility
{
    [PermissionSet(SecurityAction.LinkDemand)]
    public class Hardware
    {
        
        private static string formatSize(long lSize, bool booleanFormatOnly)
        {
            string str = "";
            NumberFormatInfo provider = new NumberFormatInfo();
            long num = 0L;
            if (lSize < 0x400L)
            {
                if (lSize == 0L)
                {
                    str = "0";
                }
                else
                {
                    str = "1";
                }
            }
            else
            {
                if (!booleanFormatOnly)
                {
                    num = lSize / 0x400L;
                }
                else
                {
                    num = lSize;
                }
                str = num.ToString("n", provider).Replace(".00", "");
            }
            return (str + " KB");
        }

        private static string formatSpeed(long lSpeed)
        {
            float num = 0f;
            NumberFormatInfo info = new NumberFormatInfo();
            if (lSpeed < 0x3e8L)
            {
                return (lSpeed.ToString() + "M Hz");
            }
            num = ((float)lSpeed) / 1000f;
            return (num.ToString() + "G Hz");
        }

        public static CPU[] GetCPU()
        {
            return CPU.Get();
        }

        public static HardDisk[] GetHardDisk()
        {
            return HardDisk.Get();
        }

        public static Memory[] GetMemory()
        {
            return Memory.Get();
        }

        public static DataTable GetSystemInfo()
        {
            return GetSystemInfo(null, null, null);
        }

        public static DataTable GetSystemInfo(string machineName, string username, string password)
        {
            DataTable table = new DataTable("SystemInfo");
            table.Columns.Add("Group", typeof(string));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Value", typeof(string));
            if (((Environment.OSVersion.Platform == PlatformID.Win32S) || (Environment.OSVersion.Platform == PlatformID.Win32Windows)) || (Environment.OSVersion.Platform == PlatformID.WinCE))
            {
                table.Rows.Add(new object[] { "*", "错误", "特征在当前系统不受支持。" });
                table.AcceptChanges();
                return table;
            }
            if (string.IsNullOrEmpty(machineName))
            {
                machineName = "localhost";
            }
            ConnectionOptions options = new ConnectionOptions();
            if (!string.IsNullOrEmpty(username))
            {
                options.Username = username;
                options.Password = password;
            }
            ManagementScope scope = new ManagementScope(@"\\" + machineName + @"\root\cimv2", options);
            try
            {
                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
                ManagementObjectCollection objects = new ManagementObjectSearcher(scope, query).Get();
                string str = "操作系统";
                foreach (ManagementObject obj2 in objects)
                {
                    table.Rows.Add(new object[] { str, "操作系统", obj2["Caption"] });
                    table.Rows.Add(new object[] { str, "版本", obj2["Version"] });
                    table.Rows.Add(new object[] { str, "制造商", obj2["Manufacturer"] });
                    table.Rows.Add(new object[] { str, "计算机名", obj2["csname"] });
                    table.Rows.Add(new object[] { str, "Windows 目录", obj2["WindowsDirectory"] });
                    table.Rows.Add(new object[] { str, "产品序号", obj2["SerialNumber"] });
                }
                query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
                objects = new ManagementObjectSearcher(scope, query).Get();
                str = "计算机";
                foreach (ManagementObject obj2 in objects)
                {
                    table.Rows.Add(new object[] { str, "制造商", obj2["Manufacturer"] });
                    table.Rows.Add(new object[] { str, "型号", obj2["model"] });
                    table.Rows.Add(new object[] { str, "类型", obj2["SystemType"] });
                    table.Rows.Add(new object[] { str, "物理内存", formatSize(long.Parse(obj2["totalphysicalmemory"].ToString()), false) });
                    table.Rows.Add(new object[] { str, "工作组/域", obj2["Domain"] });
                    table.Rows.Add(new object[] { str, "用户", obj2["UserName"] });
                }
                query = new ObjectQuery("SELECT * FROM Win32_processor");
                objects = new ManagementObjectSearcher(scope, query).Get();
                str = "处理器";
                foreach (ManagementObject obj2 in objects)
                {
                    table.Rows.Add(new object[] { str, "制造商", obj2["Manufacturer"] });
                    table.Rows.Add(new object[] { str, "类型", obj2["Caption"] });
                    table.Rows.Add(new object[] { str, "运行速度", formatSpeed(long.Parse(obj2["MaxClockSpeed"].ToString())) });
                    table.Rows.Add(new object[] { str, "缓存大小", formatSize(long.Parse(obj2["L2CacheSize"].ToString()), false) });
                }
                query = new ObjectQuery("SELECT * FROM Win32_bios");
                objects = new ManagementObjectSearcher(scope, query).Get();
                str = "BIOS";
                foreach (ManagementObject obj2 in objects)
                {
                    table.Rows.Add(new object[] { str, "BIOS", obj2["Caption"] });
                    table.Rows.Add(new object[] { str, "版本", obj2["version"] });
                }
                query = new ObjectQuery("SELECT * FROM Win32_timezone");
                objects = new ManagementObjectSearcher(scope, query).Get();
                str = "时区";
                foreach (ManagementObject obj2 in objects)
                {
                    table.Rows.Add(new object[] { str, "时区", obj2["Caption"] });
                }
                query = new ObjectQuery("SELECT * FROM Win32_LogicalMemoryConfiguration");
                objects = new ManagementObjectSearcher(scope, query).Get();
                str = "内存配置";
                foreach (ManagementObject obj2 in objects)
                {
                    table.Rows.Add(new object[] { str, "页面文件", formatSize(long.Parse(obj2["TotalPageFileSpace"].ToString()), true) });
                    table.Rows.Add(new object[] { str, "虚拟内存", formatSize(long.Parse(obj2["TotalVirtualMemory"].ToString()), true) });
                    table.Rows.Add(new object[] { str, "物理内存", formatSize(long.Parse(obj2["TotalPhysicalMemory"].ToString()), true) });
                }
                query = new ObjectQuery("SELECT * FROM Win32_NetworkConnection");
                objects = new ManagementObjectSearcher(scope, query).Get();
                str = "网络";
                foreach (ManagementObject obj2 in objects)
                {
                    table.Rows.Add(new object[] { str, "连接", obj2["Name"] });
                }
                query = new ObjectQuery("SELECT * FROM Win32_VideoController");
                objects = new ManagementObjectSearcher(scope, query).Get();
                str = "显示设备";
                foreach (ManagementObject obj2 in objects)
                {
                    table.Rows.Add(new object[] { str, "适配器", obj2["Name"] });
                    table.Rows.Add(new object[] { str, "芯片", obj2["VideoProcessor"] });
                    table.Rows.Add(new object[] { str, "模式", obj2["VideoModeDescription"] });
                    if (obj2["AdapterRAM"] == null)
                    {
                        table.Rows.Add(new object[] { str, "内存", "NONE" });
                    }
                    else
                    {
                        table.Rows.Add(new object[] { str, "内存", formatSize(long.Parse(obj2["AdapterRAM"].ToString()), false) });
                    }
                    table.Rows.Add(new object[] { str, "适配器字符串", obj2["PNPDeviceID"] });
                    table.Rows.Add(new object[] { str, "状态", obj2["Status"] });
                }
            }
            catch (Exception exception)
            {
                table.Rows.Add(new object[] { "*", "异常", exception.Message });
            }
            table.AcceptChanges();
            return table;
        }
    }



}
